package com.waiting.leetcode;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/3/19
 **/
public class BinaryTreeLevelOrderTraversal102 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        TreeNode b = new TreeNode(9);
        TreeNode c = new TreeNode(20);
        root.left = b;
        root.right = c;
        levelOrder(root);

    }

    public static List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new LinkedList<>();
        Deque<TreeNode> tmp = new LinkedList<>();
        if (root != null) {
            tmp.push(root);
        }
        while (!tmp.isEmpty()) {
            int nums = tmp.size();
            List<Integer> item = new LinkedList<>();
            for (int i = 0; i < nums; i++) {
                TreeNode oneNode = tmp.pollFirst();
                item.add(oneNode.val);
                if (oneNode.left != null) {
                    tmp.offer(oneNode.left);
                }
                if (oneNode.right != null) {
                    tmp.offer(oneNode.right);
                }
            }
            result.add(item);
        }
        return result;
    }
}
